package com.cpi.cornpp.instructionsforecasts.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.creditline.exception.CreditLineException;
import com.cpi.cornpp.customer.view.CustomerView;
import com.cpi.cornpp.instructionsforecasts.converter.InstructionsForecastsConverter;
import com.cpi.cornpp.instructionsforecasts.dao.InstructionsForecastsDAO;
import com.cpi.cornpp.instructionsforecasts.dto.InstructionsForecastsDTO;
import com.cpi.cornpp.instructionsforecasts.exception.InstructionsForecastsException;
import com.cpi.cornpp.instructionsforecasts.view.InstructionsForecastsView;

public class InstructionForecastsService {
	
	public List list(List productViews, List checkedProducts, String  customerId, int instructionPurchaseDetailId)throws InstructionsForecastsException{
		List instructionsForecastsViews = null;
		
		return instructionsForecastsViews;
	}
	/**
	 * Devuelve la lista de relaciones de instrucciones de compra
	 * @param itemId
	 * @param customerId
	 * @param instructionForecast
	 * @return
	 * @throws InstructionsForecastsException
	 */
	public List list(List productViews, CustomerView  customerView, int instructionPurchaseDetailId, int month, int year)throws InstructionsForecastsException{
		InstructionsForecastsDAO dao = null;
		ArrayList list = null;
		ArrayList aux = null;
		InstructionsForecastsConverter converter = null;
		
		dao = new InstructionsForecastsDAO();
		list = new ArrayList();
		aux = new ArrayList();
		converter = new InstructionsForecastsConverter();
		try{
			if(instructionPurchaseDetailId<=0){
				aux = (ArrayList)dao.getInstructionsForecasts(customerView.getCustomerId());
			}else{
				aux = (ArrayList)dao.getInstructionsForecasts(customerView.getCustomerId(), instructionPurchaseDetailId);
			}
			if( month == -1 ){
				list = (ArrayList)converter.convertDTOsToViews(aux, productViews);
			}else{
				list = (ArrayList)converter.convertDTOsToViewsByPeriod(aux, productViews, month, year);
			}
		}catch (InstructionsForecastsException instructionForecastException) {			
			instructionForecastException.printStackTrace(System.out);
			instructionForecastException= new InstructionsForecastsException(instructionForecastException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionForecastException;

		}
		catch (Exception e){
			InstructionsForecastsException instructionsForecastsException=null;
			e.printStackTrace(System.out);
			instructionsForecastsException= new InstructionsForecastsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw instructionsForecastsException;
		}
	
		return list;
	}
	

	/**
	 * Inserta una lista de instruction forecasts
	 * @param views
	 * @throws InstructionsForecastsException
	 */
	public void addInstructionsForecastsViews(List views) throws InstructionsForecastsException{
		for (Iterator iter = views.iterator(); iter.hasNext();) {
			InstructionsForecastsView view = (InstructionsForecastsView) iter.next();
			addInstructionsForecastsView(view);
		}
	}
	
	/**
	 * Inserta un instruction forecast
	 * @author Edgar Joao
	 * @param view
	 * @throws CreditLineException
	 */
	public void addInstructionsForecastsView(InstructionsForecastsView view) throws InstructionsForecastsException{
		InstructionsForecastsDAO dao = null;
		InstructionsForecastsConverter converter = null;
		InstructionsForecastsDTO dto = null;		
		if(view != null){
			dao = new InstructionsForecastsDAO();
			converter = new InstructionsForecastsConverter();
			dto = converter.converterInstructionsForecastsViewToDto(view);			
			try {
				dao.insertInstructionsForecast(dto);
				
			} catch (InstructionsForecastsException ife) {
				throw ife;
			} catch (Exception e) {
				InstructionsForecastsException ife = new InstructionsForecastsException(e,InstructionsForecastsException.LAYER_SERVICE,	InstructionsForecastsException.ACTION_INSERT);
				e.printStackTrace(System.out);
				throw ife;
			}				
		}
	}
	
	/**
	 * Elimina la relacion de una instruccion de compra
	 * @param id
	 * @throws InstructionsForecastsException
	 */
	public void deleteInstructionsForecastsView(int id)throws InstructionsForecastsException{
		InstructionsForecastsDAO dao = null;
		try {
			dao = new InstructionsForecastsDAO();
			dao.deleteInstructionsForecast(id);
		} catch (InstructionsForecastsException e) {
			throw e;
		}
	}
	
	/**
	 * Actualiza la relacion de instrucciones de compra
	 * @param views
	 * @param checked
	 * @param id 
	 * @throws InstructionsForecastsException
	 */
	public void updateInstructionsForecastsViews(List views, String[] checked, int id) throws InstructionsForecastsException{
		List aux = null;
		aux = Arrays.asList(checked);
		Iterator iterator = views.iterator();
		int index = 0;
		boolean containIndex = false;
		while(iterator.hasNext()){
			InstructionsForecastsView view= (InstructionsForecastsView)iterator.next();
			view.setPurchaseInstructionDetailId(id);
			containIndex = aux.contains(String.valueOf(index++));
			if(containIndex){// si esta marcado
				if( view.getForecastAdded() == 0 ){// y no esta en la base de datos entonces agregarlo
					addInstructionsForecastsView(view);
					view.setForecastAdded(1);
				}
			}else if( !containIndex){// si no esta marcado
				if( view.getForecastAdded() == 1 ){ // y estaba en la base de datos entonces borrarlo
					deleteInstructionsForecastsView(view.getInstructionForecastId());
					view.setForecastAdded(0);
				}
			}			
		}
	
	}
	
	public void markInstructionsForecastsViews(List views, String[] checked) throws InstructionsForecastsException{
		List aux = null;
		aux = Arrays.asList(checked);
		Iterator iterator = views.iterator();
		int index = 0;
		boolean containIndex = false;
		while(iterator.hasNext()){
			InstructionsForecastsView view= (InstructionsForecastsView)iterator.next();
			containIndex = aux.contains(String.valueOf(index++));
			if( !containIndex ){					
					view.setForecastChecked(0);
			}else{							
					view.setForecastChecked(1);
			}			
		}
	}
}
